const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

/**
 * 模糊匹配
 * @param {*} list 
 * @param {*} keyWord 
 * @returns 
 */
const fuzzyQuery = function (list, keyWord) {
  var arr = [];
  for (var i = 0; i < list.length; i++) {
    if (list[i].address.indexOf(keyWord) >= 0 || list[i].storeName.indexOf(keyWord) >= 0) {
      arr.push(list[i]);
    }
  }
  return arr;
}

/**
 * 对比两个对象的值是否完全相等 返回值 true/false
 */
const isObjectValueEqual = function (a, b) {
  //取对象a和b的属性名
  var aProps = Object.getOwnPropertyNames(a);
  var bProps = Object.getOwnPropertyNames(b);
  //判断属性名的length是否一致
  if (aProps.length != bProps.length) {
    return false;
  }
  //循环取出属性名，再判断属性值是否一致
  for (let i = 0; i < aProps.length; i++) {
    let propName = aProps[i];
    // console.log(a, b);
    if (a[propName] !== b[propName]) {
      if (aProps[i] == "realName" || aProps[i] == "idNo" || aProps[i] == "origo") {
        return aProps[i]
      } else {
        return false
      }
    }
  }
  return true;
}



/**
 * 检查对象某个键值是否为空
 */
const traverseObject = function (object) {
  for (const key in object) {
    if (object[key] == "" || object[key] == undefined) {
      return key
    }
  }
}

// 去除相同的值
function duplicateRemoval(arr) {
  let newsArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (newsArr.indexOf(arr[i]) === -1) {
      newsArr.push(arr[i]);
    }
  }
  return newsArr;
}

/**
 * 
 * @param {Array} arr 数组
 * @param {String} id 数组的唯一值
 */
function distinct(arr, id) {
  let newArr = [];
  var obj = {};
  for (let i = 0; i < arr.length; i++) {
    if (!obj[arr[i][id]]) {
      newArr.push(arr[i]);
      obj[arr[i][id]] = true;
    }
  }
  return newArr
}

//  数组排序
function newSort(arr) {

}

// 数组对象深拷贝
function copy(obj) {
  var newobj = obj.constructor === Array ? [] : {};
  if (typeof obj !== 'object') {
    return;
  }
  for (var i in obj) {
    newobj[i] = typeof obj[i] === 'object' ? copy(obj[i]) : obj[i];
  }
  return newobj
}

// 防抖函数
function debounce(func, wait) {
  let timer;
  return function () {
    let context = this; // 这边的 this 指向谁?
    let args = arguments; // arguments中存着e
    if (timer) clearTimeout(timer);
    let callNow = !timer;
    timer = setTimeout(() => {
      timer = null;
    }, wait)
    if (callNow) func.apply(context, args);
  }
}

// 节流
function throttle(func, wait) {
  let timeout;
  return function () {
    let context = this;
    let args = arguments;
    if (!timeout) {
      timeout = setTimeout(() => {
        timeout = null;
        func.apply(context, args)
      }, wait)
    }
  }
}

function formatSeconds(value) {
  let theTime = parseInt(value);
  let theTime1 = 0;
  let theTime2 = 0;

  if (theTime >= 60) {
    theTime1 = parseInt(theTime / 60);
    theTime = parseInt(theTime % 60);
    if (theTime1 >= 60) {
      theTime2 = parseInt(theTime1 / 60);
      theTime1 = parseInt(theTime1 % 60);
    }
  }
  if (theTime < 10) {
    theTime = "0" + parseInt(theTime)
  }
  let result = {
    hour: 0,
    minute: 0
  }
  // 分
  if (theTime1 >= 0) {
    if (theTime1 < 10) {
      theTime1 = parseInt(theTime1)
    }

    result.minute = theTime1
  }
  // 时
  if (theTime2 >= 0) {
    if (theTime2 < 10) {
      theTime2 = parseInt(theTime2)
    }
    result.hour = theTime2
  }
  return result;
}

module.exports = {
  formatTime,
  isObjectValueEqual,
  traverseObject,
  duplicateRemoval,
  newSort,
  copy,
  debounce,
  throttle,
  formatSeconds,
  distinct
}